package com.how2java.tmall.controller;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.service.CategoryService;
import com.how2java.tmall.util.ImageUtil;
import com.how2java.tmall.util.Page;
 
@Controller
@RequestMapping("")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
  
    @RequestMapping("admin_category_list")
    public String list(Model model,Page page){
    	PageHelper.offsetPage(page.getStart(),page.getCount());
        List<Category> cs= categoryService.list();
        int total =(int) new PageInfo<>(cs).getTotal();
        page.setTotal(total);
        model.addAttribute("cs", cs);
        model.addAttribute("page", page);
        return "admin/listCategory";
    }
    
    /**
     * 添加新的分类
     * @param c
     * @param session
     * @param image
     * @return
     * @throws IOException
     */
    @RequestMapping("admin_category_add")
    public String add(Category c, HttpSession session, MultipartFile image) throws IOException {
        categoryService.add(c);
        //保存在服务器上
        writeJPG(session,c.getId()+"",session.getServletContext().getRealPath("img/category/")+"\\",image);
        //保存在本地,开发环境中使用的
        writeJPG(session,c.getId()+"","G:\\eclipse-workspace\\tmall_ssm\\src\\main\\webapp\\img\\category\\",image);
        
        return "redirect:/admin_category_list";
    }
    
    /**
     * 删除分类
     * @param session
     * @param id
     * @return
     */
    @RequestMapping("admin_category_delete")
    public String delete(HttpSession session,Integer id) {
    	categoryService.delete(id);
    	//删除服务器端的图片
    	File  imageFolder= new File(session.getServletContext().getRealPath("img/category/"));
	    File file = new File(imageFolder,id+".jpg");
	    file.delete();
	    //删除本地端的图片,只在开发环境中使用
	    imageFolder = new File("G:\\eclipse-workspace\\tmall_ssm\\src\\main\\webapp\\img\\category\\");
	    file = new File(imageFolder,id+".jpg");
	    file.delete();
	    
	    
    	return "redirect:/admin_category_list";
    }
    
    /**
     * 编辑页面初始化
     * @param id
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("admin_category_edit")
    public String edit(int id,Model model) throws IOException {
        Category c= categoryService.get(id);
        model.addAttribute("c", c);
        return "admin/editCategory";
    }
    
    
    @RequestMapping("admin_category_update")
    public String update(Category c, HttpSession session, MultipartFile image) throws IOException {
        categoryService.update(c);
        if(null!=image &&!image.isEmpty()){
        	//保存在服务器上
            writeJPG(session,c.getId()+"",session.getServletContext().getRealPath("img/category/")+"\\",image);
            //保存在本地,开发环境中使用的
            writeJPG(session,c.getId()+"","G:\\eclipse-workspace\\tmall_ssm\\src\\main\\webapp\\img\\category\\",image);
        }
        return "redirect:/admin_category_list";
    }
    
    
    
    
    
    /**
     * 根据路径保存图片(服务器或本地项目路径)
     * @param session
     * @param jpgName
     * @param url
     * @param uploadedImageFile
     * @throws IOException
     */
    public void writeJPG(HttpSession session,String jpgName,String url, MultipartFile image) throws  IOException {
//    	File  imageFolder= new File(url);
//        File file = new File(imageFolder,jpgName+".jpg");
//        if(!file.getParentFile().exists())
//            file.getParentFile().mkdirs();
//        image.transferTo(file);
//        BufferedImage img = ImageUtil.change2jpg(file);
//        ImageIO.write(img, "jpg", file);
    	
        InputStream is = null;
        OutputStream os = null;
        os = new FileOutputStream(url+jpgName+".jpg");
        is = image.getInputStream();
        IOUtils.copy(is, os);
        
    }
    
    
}